﻿using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using DAL;
using BLL;
using System.Text;


public partial class Plublic_TermCurrentAccount : System.Web.UI.Page
{

    string strConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    TermCurrentHandler objTermCurrentHandler = new TermCurrentHandler();
    LocationHandler objLocationHandler = new LocationHandler();
    UsersHandler objUsersHandler = new UsersHandler();
    InterestRatesHandler objInterestRatesHandler = new InterestRatesHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindDropDowListAccHolders();
            this.BindDropDowListTerm();
            this.BindDropDowListLocation();
        }
    }

    protected void BindDropDowListAccHolders()
    {
        List<Users> lstUsers = new List<Users>();

        lstUsers = objUsersHandler.GetUsersList(strConn);

        ddlAccHolder.DataSource = lstUsers;
        ddlAccHolder.DataValueField = "UserID";
        ddlAccHolder.DataTextField = "FullName";
        ddlAccHolder.DataBind();

        
    }

    protected void BindDropDowListLocation()
    {
        List<Location> lstLocation = new List<Location>();

        lstLocation = objLocationHandler.GetLocationList(strConn);

        ddlLocation.DataSource = lstLocation;
        ddlLocation.DataValueField = "LocationID";
        ddlLocation.DataTextField = "Address";
        ddlLocation.DataBind();
    }

    protected void BindDropDowListTerm()
    {
        List<InterestRates> lstInterestRates = new List<InterestRates>();

        lstInterestRates = objInterestRatesHandler.GetInterestRatesList(strConn); ;

        ddlTerm.DataSource = lstInterestRates;
        ddlTerm.DataValueField = "InterestRatesID";
        ddlTerm.DataTextField = "RateType";
        ddlTerm.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        /*
         
         DateTime d1 = DateTime.ToDate;
string dd = d1.Day.ToString();
string mm = d1.Month.ToString();
string yyyy = d1.Year.ToString();

string d2 = dd + "/" + mm + "/" + yyyy;
         
         */
        TermCurrent objTermCurrent = new TermCurrent();

        objTermCurrent.Created = DateTime.Now.ToString();
        objTermCurrent.Updated = DateTime.Now.ToString();
        objTermCurrent.Isactive = "1";
        objTermCurrent.AccountNumber = txtAccounNumber.Text.ToString();
        objTermCurrent.Term = Convert.ToInt32(ddlTerm.Text);
        objTermCurrent.UserID = Convert.ToInt32(ddlAccHolder.SelectedValue);
        objTermCurrent.Opendate = txtOpenDate.Text.ToString();
        objTermCurrent.Balance = Convert.ToDouble(txtBalance.Text);

        DateTime d1 = calEventDate.SelectedDate;

        string day = d1.Day.ToString();
        string month = "";
        string year="";
        int mm = (d1.Month + Convert.ToInt32(ddlTerm.SelectedItem.Text));
        if (mm < 12)
        {
            month = mm.ToString();
            year = d1.Year.ToString();
        }
        else 
        {
             int yy = mm/12;
             mm = mm - 12*yy;
             month = mm.ToString();
             year = (d1.Year + yy).ToString();
        }
        string d2 = month + "/" + day + "/" + year;
        objTermCurrent.MaturityDate = d2;

        objTermCurrent.Currency = ddlCurrency.Text.ToString();
        objTermCurrent.InterestRateID = Convert.ToInt32(ddlTerm.SelectedValue);
        objTermCurrent.LocationID = Convert.ToInt32(ddlLocation.SelectedValue);
        if (cboStatus.Checked == true)
        {
            objTermCurrent.Status = "1";
        }
        else
        {
            objTermCurrent.Status = "0";
        }
        objTermCurrent.AccountHolders = ddlAccHolder.SelectedItem.ToString();
        objTermCurrent.Descriptions = txtDescriptions.Text.ToString();


        if (objTermCurrentHandler.AddNewTermCurrent(objTermCurrent, strConn))
        {
            this.SetText();
        }
    }

    protected void SetText()
    {
        txtAccounNumber.Text = "";
        txtBalance.Text = "";
        txtDescriptions.Text = "";
        txtOpenDate.Text = "";
    }
    protected void calEventDate_SelectionChanged(object sender, System.EventArgs e)
    {
        txtOpenDate.Text = calEventDate.SelectedDate.ToString("MM/dd/yyyy");
    }
   
    protected void ddlTerm_Load(object sender, System.EventArgs e)
    {
        string rateType = ddlTerm.SelectedItem.ToString();
        if (rateType != string.Empty)
        {
            InterestRates objInterestRates = new InterestRates();
            objInterestRates = objInterestRatesHandler.GetInterestRates(rateType, strConn);
            if (objInterestRates != null)
            {
                txtIRate.Text = objInterestRates.InterestRate.ToString(); ;
            }
        }
    }
    

    protected void ddlTerm_SelectedIndexChanged(object sender, System.EventArgs e)
    {

        string rateType = ddlTerm.SelectedItem.Text.ToString();
        if (rateType != string.Empty)
        {

            InterestRates objInterestRates = new InterestRates();
            objInterestRates = objInterestRatesHandler.GetInterestRates(rateType, strConn);
            if (objInterestRates != null)
            {
                txtIRate.Text = objInterestRates.InterestRate.ToString(); ;
            }
        }

    }
}